// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Z [author] rhyme_lph
// H [email]  rhymelph@gmail.com
// h [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/src/entities/batch_invenotries_entity.dart';
import 'package:finance_app/src/entities/ocr_attachment_entity.dart';
import 'package:finance_app/src/http_service/http_inventory_service.dart';
import 'package:finance_app/src/mixin/from_attach_mixin.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/page/dashboard/products_services/inventory_adjustment_page.dart';

class InventoryAdjustmentProvider extends BaseProvider<InventoryEntity>
    with FormAttachMixin {
  String currentSelect = 'Return Item';

  TextEditingController additionalController = TextEditingController();
  FocusNode additionalFocusNode = FocusNode();

  String batchId;

  List<BatchInvenotriesEntity> batchInventories;

  int id;

  @override
  Future get dataFuture =>
      id != null ? HttpInventoryService.showInventory(id) : null;

  @override
  void dispose() {
    super.dispose();
    additionalController.dispose();
    additionalFocusNode.dispose();
  }

  void onChangeSelect(String e) {}

  void onChnageAdjustMentDate() {}

  @override
  FutureOr<InventoryEntity> transformData(InventoryEntity d, bool isCache) {
    initData(null, d);
    return super.transformData(d, isCache);
  }

  void initData(String batchId, InventoryEntity data) {
    this.batchId = batchId;
    this.data = data;
    currentSelect = data.type == 'return' ? 'Return Item' : 'Stock Adjustment';
    additionalController.text = data.notes?.toString();
    if (data.batchInventories != null && data.batchInventories.length > 0) {
      batchInventories = data.batchInventories;
    } else {
      batchInventories = [
        BatchInvenotriesEntity()
          ..name = data.name
          ..entityName = data.entityName
          ..inventoryLeft = data.inventoryLeft
          ..inventoriableId = data.inventoriableId
          ..inventoriableType = data.inventoriableType
          ..type = data.type
          ..quantity = data.quantity
      ];
    }
  }

  @override
  List<OcrAttachmentEntity> get attachments => data?.attachments;

  @override
  bool get isEdit => false;

  @override
  void unFocus() {
    primaryFocus.unfocus();
  }
}
